package com.neuedu.yyzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neuedu.yyzx.entity.Menu;
import com.neuedu.yyzx.entity.RoleMenu;
import com.neuedu.yyzx.mapper.RoleMenuMapper;
import com.neuedu.yyzx.service.IMenuService;
import com.neuedu.yyzx.service.IRoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 角色权限 服务实现类
 * </p>
 *
 * @author 第7组....
 * @since 2021-05-22
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements IRoleMenuService {
    @Resource
    IMenuService umsPermissionService;
    @Override
    public List<Menu> getPermission() {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        wrapper.eq("active",1);
        List<Menu> list = umsPermissionService.list(wrapper);
        for(Menu permission : list) {
            getChildren(permission);
        }
        return list;
    }

    @Override
    public RoleMenu getByRoleId(Integer roleId) {
        QueryWrapper<RoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        return this.getOne(wrapper);
    }

    /**
     * 参数为父权限对象
     * 通过父权限获取子权限
     *
     * */
    private void getChildren(Menu umsPermission) {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",umsPermission.getId());
        wrapper.eq("active",1);
        List<Menu> list = umsPermissionService.list(wrapper);
        for(Menu permission : list) {
//            if(permission.getType() != 0) {
            getChildren(permission);
//            }
        }
        umsPermission.setChildren(list);
    }
}
